import * as goodsApi from '@/api/goods.js';

export default {
  namespaced: true,
  state() {
    return {
      subCategoryGoods: {
        items: [],
        page: 1,
        pageSize: 10,
        counts: 0,
        pages: 1,
      },
      subCategoryFilter: {
        brands: [],
        categories: [],
        goods: [],
        saleProperties: [],
      },
      // 商品详情
      goodsDetail: {},
    };
  },
  getters: {
    subCategoryFilters(state) {
      return [
        {
          id: 'pinpai',
          name: '品牌',
          properties: state.subCategoryFilter.brands,
        },
        ...state.subCategoryFilter.saleProperties,
      ];
    },
  },
  mutations: {
    // 懒加载拼接数据
    SET_SUB_CATEGORY_GOODS(state, newValue) {
      state.subCategoryGoods = {
        ...newValue,
        items: state.subCategoryGoods.items.concat(newValue.items),
      };
    },
    // 重置数据
    RESET_SUB_CATEGORY_GOODS(state, newValue) {
      state.subCategoryGoods = newValue;
    },
    // 筛选器
    SET_SUB_CATEGORY_FILTER(state, newValue) {
      state.subCategoryFilter = newValue;
    },
    // 商品详情
    SET_GOODS_DETAIL(state, newValue) {
      state.goodsDetail = newValue;
    },
  },
  actions: {
    async updateSubCategoryGoods({ commit }, params) {
      const res = await goodsApi.getSubCategoryGoods(params);
      commit('SET_SUB_CATEGORY_GOODS', res);
    },
    async resetSubCategoryGoods({ commit }, params) {
      const res = await goodsApi.getSubCategoryGoods(params);
      commit('RESET_SUB_CATEGORY_GOODS', res);
    },
    async updateSubCategoryFilter({ commit }, id) {
      const res = await goodsApi.getSubCategoryFilter(id);
      commit('SET_SUB_CATEGORY_FILTER', res);
    },
    async updateGoodsDetail({ commit }, id) {
      const res = await goodsApi.getGoodsDetail(id);
      commit('SET_GOODS_DETAIL', res);
    },
  },
};
